Imports System
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports C1.Win.C1FlexGrid
Imports C1.C1Preview
Imports ACFlexToText

Public Class FlexToTex
   Inherits System.Windows.Forms.Form

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub



   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
      'populate grid
      Dim rnd As New Random()
      _flex.Rows.Count = 14
      _flex(0, 1) = "Direction"
      _flex(0, 2) = "Region"
      Dim rg As CellRange = _flex.GetCellRange(0, 3, 0, 4)
      rg.Data = "Rnd"
      Dim rg1 As CellRange = _flex.GetCellRange(0, 5, 0, 7)
      rg1.Data = "Hola"
      Dim rg2 As CellRange = _flex.GetCellRange(0, 8, 0, _flex.Cols.Count - 1)
      rg2.Data = "Cruel"
      Dim r As Integer
      For r = 1 To _flex.Rows.Count - 1
         _flex(r, 0) = r
         If r < 7 Then
            _flex(r, 1) = "Inbound"
         Else
            _flex(r, 1) = "Outbound"
         End If

         If r < 3 Then
            _flex(r, 2) = "North"
         ElseIf r < 7 Then
            _flex(r, 2) = "South"
         ElseIf r < 10 Then
            _flex(r, 2) = "East"
         Else
            _flex(r, 2) = "West"
         End If

         Dim c As Integer
         For c = 3 To _flex.Cols.Count - 1
            _flex(r, c) = 10 'rnd.Next(1000)
            _flex.Cols(c).Format = "#,###"
         Next
      Next

      'set up styles
      Dim s As CellStyle = _flex.Styles(CellStyleEnum.Subtotal0)
      s.BackColor = Color.Black
      s.ForeColor = Color.White
      s.Font = New Font(_flex.Font, FontStyle.Bold)

      s = _flex.Styles(CellStyleEnum.Subtotal1)
      s.BackColor = Color.DarkBlue
      s.ForeColor = Color.White
      s = _flex.Styles(CellStyleEnum.Subtotal2)
      s.BackColor = Color.DarkRed
      s.ForeColor = Color.White

      'more setup
      _flex.AllowDragging = AllowDraggingEnum.None
      _flex.AllowEditing = True
      _flex.Cols(0).WidthDisplay \= 3
      _flex.Tree.Column = 1

      'show totals
      UpdateTotals()
   End Sub

   Private Sub UpdateTotals()
      'clear existing totals
      _flex.Subtotal(AggregateEnum.Clear)

      'calculate subtotals (three levels, totals on every column)
      Dim c As Integer
      For c = 3 To _flex.Cols.Count - 1
         If _chkGrand.Checked Then
            _flex.Subtotal(AggregateEnum.Sum, 0, -1, c, "Grand Total")
         End If
         If _chkDirection.Checked Then _flex.Subtotal(AggregateEnum.Sum, 1, 1, c, "Total for {0}")
         If _chkRegion.Checked Then _flex.Subtotal(AggregateEnum.Sum, 2, 2, c, "Total for {0}")
      Next

      'done, autosize columns to finish
      _flex.AutoSizeCols()

   End Sub

   Private Sub _chk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _chkAbove.CheckedChanged, _chkDirection.CheckedChanged, _chkGrand.CheckedChanged, _chkRegion.CheckedChanged
      'set subtotal position
      If _flex.SubtotalPosition = _chkAbove.Checked Then
         _flex.SubtotalPosition = SubtotalPositionEnum.AboveData
      Else
         _flex.SubtotalPosition = SubtotalPositionEnum.BelowData
      End If

      UpdateTotals()
   End Sub

   Private Sub _btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _btnPrint.Click
      Dim pd As PrintDocument = _flex.PrintParameters.PrintDocument
      pd.DefaultPageSettings.Landscape = True
      Dim m As Margins = pd.DefaultPageSettings.Margins
      m.Left = 25
      m.Right = 25
      m.Top = 25
      m.Bottom = 25
      _flex.PrintGrid("Subtotals", PrintGridFlags.ShowPreviewDialog Or PrintGridFlags.FitToPage)

   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Try
         _flex.Rows.Fixed = 3
         Dim x As New FlexToText(_flex, 1, 2, " ", "|")
         Dim align() As AlignHorzEnum = {AlignHorzEnum.Center, AlignHorzEnum.Right, AlignHorzEnum.Center, AlignHorzEnum.Left}
         x.AlignColumns = align
         Dim subtitulos() As String = {""}
         Dim show() As Integer = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1}
         'x.showColumns = show
         'x.HoraFecha = True
         x.setCabecera("Sucursal", "x", subtitulos)
         x.PrintSucHora = False
         RichTextBox1.Text = x.execute().ToString()
      Catch ex As Exception

      End Try
   End Sub
End Class
